package com.xiaowu.netmusic.controller;

import com.xiaowu.netmusic.common.constant.MessageConstant;
import com.xiaowu.netmusic.service.AliOssService;
import com.xiaowu.netmusic.utils.AliOssUtil;
import com.xiaowu.netmusic.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @description:
 * @author: xiaowu
 * @time: 2025/4/28 20:09
 */
@RestController
@Tag(name = "通用接口")
@Slf4j
@RequestMapping("/user")
@AllArgsConstructor
public class CommonController {


    private  final  AliOssUtil aliOssUtil;
    private  final AliOssService aliOssService;


    @Operation(summary = "上传阿里云接口")
    @PostMapping("/upload/avatar")
    public Result<String> upload(@RequestParam(value = "avatar") MultipartFile file) {
        log.info("文件上传：{}", file);

        try {
            // 原始文件名
            String originalFilename = file.getOriginalFilename();
            // 截取原始文件名的后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            // 构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;

            // 文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            System.out.println(filePath);
            log.info("{}",filePath);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }

        return Result.error(MessageConstant.UPLOAD_FAILED);

    }

    @Operation(summary = "上传音乐文件到阿里云 OSS")
    @PostMapping("/upload/audio")
    public Result<String> uploadAudio(@RequestParam("music") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("上传文件不能为空");
        }

        // 允许的音频扩展名
        List<String> allowedExtensions = Arrays.asList(".mp3", ".wav", ".flac", ".aac", ".ogg", ".m4a");

        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null || !originalFilename.contains(".")) {
            return Result.error("文件名不合法");
        }

        // 校验扩展名
        String extension = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
        if (!allowedExtensions.contains(extension)) {
            return Result.error("仅支持音频文件类型：" + allowedExtensions);
        }

        // 校验 MIME 类型
        String contentType = file.getContentType(); // e.g., audio/mpeg
        if (contentType == null || !contentType.startsWith("audio/")) {
            return Result.error("上传的文件类型不是音频文件，实际类型：" + contentType);
        }

        try {
            String filePath = aliOssService.uploadAudio(file);
            return Result.success("上传成功", filePath);
        } catch (IOException e) {
            log.error("音乐上传失败", e);
            return Result.error("音乐上传失败，请稍后重试");
        }
    }


}
